package com.zhang.infrastructure.model;


import com.zhang.infrastructure.model.constant.ExceptionConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2023-03-14 17:37
 **/
@Slf4j
public class KingHoodExceptionUtil {

    /**
     * 抛出异常
     *
     * @param msg 异常信息
     * @author zfj
     * @date 2023/3/14
     */
    public static void throwException(String msg){
        log.error(msg);
        throw new KingHoodException(ExceptionConstant.CODE_BAD_SERVER,msg);
    }

    /**
     * 抛出异常
     *
     * @param msg 异常信息
     * @author zfj
     * @date 2023/3/14
     */
    public static void throwException(Integer code,String msg){
        log.error("抛出异常:code:{},msg:{}",code,msg);
        throw new KingHoodException(code,msg);
    }

    /**
     * 为真的时候抛出异常
     *
     * @param in 入参
     * @param msg 异常信息
     * @author zfj
     * @date 2023/3/14
     */
    public static void trueThrows(boolean in,String msg){
        if(in){
            log.error(msg);
            throw new KingHoodException(ExceptionConstant.CODE_BAD_SERVER,msg);
        }
    }

    /**
     * 判断null
     *
     * @param in 入参
     * @param msg 异常信息
     * @author zfj
     * @date 2023/3/14
     */
    public static void isNull(Object in,String msg){
        if(Objects.isNull(in)){
            log.error(msg);
            throw new KingHoodException(ExceptionConstant.CODE_BAD_SERVER,msg);
        }
    }

    /**
     * 判断null
     *
     * @param in 入参
     * @param msg 异常信息
     * @param logInfo 打印日志
     * @author zfj
     * @date 2023/3/14
     */
    public static void isNull(Object in,String msg,String logInfo){
        if(Objects.isNull(in)){
            if(Strings.isNotBlank(logInfo)){
                log.error(logInfo);
            }
            log.error(msg);
            throw new KingHoodException(ExceptionConstant.CODE_BAD_SERVER,msg);
        }
    }

    /**
     * 判断空字符串
     *
     * @param in 入参
     * @param msg 异常信息
     * @author zfj
     * @date 2023/3/14
     */
    public static void isBlank(String in,String msg){
        if(StringUtils.isBlank(in)){
            log.error(msg);
            throw new KingHoodException(ExceptionConstant.CODE_BAD_SERVER,msg);
        }
    }

    /**
     * 判断Empty
     *
     * @param list 入参
     * @param msg 异常信息
     * @author zfj
     * @date 2023/3/14
     */
    public static <T> void isEmpty(Collection<T> list, String msg){
        if(CollectionUtils.isEmpty(list)){
            log.error(msg);
            throw new KingHoodException(ExceptionConstant.CODE_BAD_SERVER,msg);
        }
    }

    /**
     * 判断Empty
     *
     * @param list 入参
     * @param msg 异常信息
     * @author zfj
     * @date 2023/3/14
     */
    public static <T> void isEmpty(Collection<T> list,Integer code, String msg){
        if(CollectionUtils.isEmpty(list)){
            log.error(msg);
            throw new KingHoodException(code,msg);
        }
    }

    /**
     * 判断Empty
     *
     * @param map 入参
     * @param msg 异常信息
     * @author zfj
     * @date 2023/3/14
     */
    public static <T> void isEmpty(Map map, String msg){
        if(CollectionUtils.isEmpty(map)){
            log.error(msg);
            throw new KingHoodException(ExceptionConstant.CODE_BAD_SERVER,msg);
        }
    }
}
